#include <iostream>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <ctime>
void test_unordered_map()
{
    std::unordered_map<std::string, std::string> dict;
    dict.insert(std::make_pair("sort", "排序"));
    dict["string"] = "字符串";
    dict.insert(std::make_pair("left", "左边"));
    std::unordered_map<std::string, std::string>::iterator it = dict.begin();
    while (it != dict.end())
    {
        std::cout << it->first << " : " << it->second << std::endl;
        ++it;
    }
    std::cout << std::endl;
}
void test_map()
{
    std::map<std::string, std::string> dict;
    dict.insert(std::make_pair("sort", "排序"));
    dict["string"] = "字符串";
    dict.insert(std::make_pair("left", "左边"));
    std::map<std::string, std::string>::iterator it = dict.begin();
    while (it != dict.end())
    {
        std::cout << it->first << " : " << it->second << std::endl;
        ++it;
    }
    std::cout << std::endl;
}
void test_unordered_set()
{
    std::unordered_set<int> us;
    us.insert(4);
    us.insert(2);
    us.insert(1);
    us.insert(5);
    us.insert(6);
    us.insert(2);
    std::unordered_set<int>::iterator it = us.begin();
    while (it != us.end())
    {
        std::cout << *it << " ";
        ++it;
    }
    std::cout << std::endl;
}
void test_set()
{
    std::set<int> s;
    s.insert(4);
    s.insert(2);
    s.insert(1);
    s.insert(5);
    s.insert(6);
    s.insert(2);
    std::set<int>::iterator it = s.begin();
    while (it != s.end())
    {
        std::cout << *it << " ";
        ++it;
    }
    std::cout << std::endl;
}
void test()
{
    std::unordered_set<int> us;
    std::set<int> s;
    const int n = 10000000;
    std::vector<int> v;
    v.reserve(n);
    srand(time(0));
    for(size_t i = 0;i<n;i++)
    {
        v.push_back(rand());
    }
    size_t begin1 = clock();
    for(size_t i = 0;i<n;i++)
    {
        us.insert(v[i]);
    }
    size_t end1 = clock();
    std::cout<<"unordered_set : "<<end1-begin1<<std::endl;
    size_t begin2 = clock();
    for(size_t i = 0;i<n;i++)
    {
        s.insert(v[i]);
    }
    size_t end2 = clock();
    std::cout<<"set : "<<end2-begin2<<std::endl;

    size_t begin3 = clock();
    for(size_t i = 0;i<n;i++)
    {
        us.find(v[i]);
    }
    size_t end3 = clock();
    std::cout<<"unordered_set : "<<end3-begin3<<std::endl;
    size_t begin4 = clock();
    for(size_t i = 0;i<n;i++)
    {
        s.find(v[i]);
    }
    size_t end4 = clock();
    std::cout<<"set : "<<end4-begin4<<std::endl;
}
int main()
{
    test_unordered_map();
    test_map();
    test_unordered_set();
    test_set();
    test();
    return 0;
}